<?php
/***************************************************************************** *
* Class Name: DBMgrDef
* Author: Nicholas Malacarne
* Description: Used to access/manage a database.
***************************************************************************** */
class DBMgrDef {

    private $connection,
            $db_select;


/***************************************************************************** *
*********************************CONSTRUCTOR!******************************** *
* Author: Nicholas Malacarne
* Parameters: $serv - The name of the database server.
*             $acc - The account name for the server.
 *            $pass - The password for the server.
 *            $db - the database to select.
* Description: Basic set-up for PlayerDef obj. creation.
***************************************************************************** */
    function __construct($serv, $acc, $pass, $db)
    {
     /* Connects to database server.
      */$this->connection = mysql_connect($serv, $acc, $pass) or
                     die('Could not connect to server:' . mysql_error());

     /* Selects the correct database.
      */$this->db_select = mysql_select_db($db, $this->connection) or
                           die('Could not select database:' . mysql_error());
    }

    function  __destruct()
    {
        mysql_close($this->connection) or 
        die('Could not close DB connection: '. mysql_error());
    }

/***************************************************************************** *
* Function Name: fetchRow
* Author: Nicholas Malacarne
* Parameters: $tName - The table name.
 *            $rName - The row name.
 *            $cName - The column name.
* Description: Returns an associative array from the given row.
***************************************************************************** */
    public function fetchRow($tName, $rName, $cName = "name")
    {
     /* Grabs the specified row from the specified table.
      */$result = mysql_query("SELECT * FROM $tName WHERE $cName = '$rName'") or
                  die("Error reading table:\n" . mysql_error());

     /* Returns the row as an assoc array.
      */return mysql_fetch_assoc($result);
    }

/***************************************************************************** *
* Function Name: updateRow
* Author: Nicholas Malacarne
* Parameters: $tName - The table name.
 *            $rName - The row name.
 *            $cName - The column name.
 *            $data  - Data to write into DB.
* Description: Updates a row/col of a table.
***************************************************************************** */
    public function updateRow($tName, $rName, $cName, $data)
    {
        $result = mysql_query("UPDATE $tName 
                               SET $cName = '$data'
                               WHERE name = '$rName'") or 
                  die ("Error updating row: ". mysql_error());
    }

/***************************************************************************** *
* Function Name: getRows
* Author: Nicholas Malacarne
* Parameters: $tName - The table name.
* Description: Returns the amount of rows in a table.
***************************************************************************** */
    public function getRows($tName)
    {
        $result = mysql_query("SELECT * FROM $tName");
        $num_rows = mysql_num_rows($result);

        return $num_rows;
    }
}
?>
